package com.cskaoyan.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName Md5Utils
 * @Description
 * @Author Lxz
 * @Date 2022-8-18 9:49
 */
public class Md5Utils {
    public static String getMd5(String content) throws NoSuchAlgorithmException {

        MessageDigest messageDigest = MessageDigest.getInstance("md5");
        byte[] bytes = content.getBytes();
        byte[] digest = messageDigest.digest(bytes);
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : digest) {
            // 任意整型 & 0xff = 本身
            // 字节 & 0xff = 转成int
            // 将byte从（-128 至 127）转化成 int（转化后的数值范围： 0 至 255）
            int i = b & 0xff;
            String s = Integer.toHexString(i);
            //0a
            if (s.length() == 1){
                stringBuffer.append(0);
            }
            stringBuffer.append(s);

        }
        return stringBuffer.toString();

    }

}
